﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using MyPhotoIndex.Plugins.WebHost.Security;

namespace MyPhotoIndex.Plugins.WebHost.VirtualPages
{
    public class LoginPage : VirtualPage
    {
        public override VirtualPage Clone()
        {
            return new LoginPage();
        }

        public override void LoadPage()
        {
            try
            {
                if (FormVars.Count >= 2)
                {
                    string userName = FormVars["userName"];
                    string password = FormVars["password"];

                    bool isAuthenticated = Authentication.Authenticate(Response, userName, password);
                    if (isAuthenticated == true)
                    {
                        string redirectUrl = Request.QueryString["src"];
                        redirectUrl = System.Web.HttpUtility.UrlDecode(redirectUrl);
                        Response.Redirect(redirectUrl);
                    }
                }
            }
            catch
            {
            }
        }

        public override void RenderPage(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Rel, "stylesheet");
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/css");
            writer.AddAttribute(HtmlTextWriterAttribute.Href, "/skin/skin.css");
            writer.RenderBeginTag(HtmlTextWriterTag.Link);
            writer.RenderEndTag();

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "loginPageMainTable");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "100%");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);

            //row 1
            writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "100px");
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);

            //logo area
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "logoCell");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, "logo");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, "logoText"); 
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.Write("My Photo Index<br>");
            writer.AddAttribute(HtmlTextWriterAttribute.Id, "logoVersionString");
            writer.RenderBeginTag(HtmlTextWriterTag.Font);
            string version = System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString();
            writer.Write(version);
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderEndTag();//td
            // end logo area

            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.RenderEndTag();//td

            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.RenderEndTag();//td

            writer.RenderEndTag();//tr
            //end row 1

            //row 2
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);

            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "50%");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("&nbsp;");
            writer.RenderEndTag();//td

            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "380px");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "300px"); 
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            RenderLoginForm(writer);
            writer.RenderEndTag();//td

            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "50%");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("&nbsp;");
            writer.RenderEndTag();//td

            writer.RenderEndTag();//tr
            //end row 2

            //row 3
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);

            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.RenderEndTag();//td

            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.RenderEndTag();//td

            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.RenderEndTag();//td

            writer.RenderEndTag();//tr
            //end row 3

            writer.RenderEndTag();//table


            writer.Write("<script type=\"text/javascript\" language=\"JavaScript\">");
            writer.Write("document.getElementById('userName').focus();");
            writer.Write("</script>");
        }

        private void RenderLoginForm(HtmlTextWriter writer)
        {
            writer.AddStyleAttribute(HtmlTextWriterStyle.VerticalAlign, "top");
            writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign, "center");
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "loginForm");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            writer.AddAttribute(HtmlTextWriterAttribute.Id, "loginForm");
            writer.AddAttribute("method", "post");
            writer.RenderBeginTag(HtmlTextWriterTag.Form);

            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            RenderInputField(writer, "userName", 1, 16, "User Name *", "", false);

            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Td);//second column
            writer.RenderEndTag();//second column

            writer.RenderEndTag();//tr

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            RenderInputField(writer, "password", 2, 16, "Password *", "", true);

            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Td);//second column
            writer.RenderEndTag();//second column

            writer.RenderEndTag();//tr

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);

            writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
            writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign, "right");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            writer.AddAttribute(HtmlTextWriterAttribute.Type, "submit");
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "button");
            writer.AddAttribute(HtmlTextWriterAttribute.Name, "submit");
            writer.AddAttribute(HtmlTextWriterAttribute.Value, "Login");
            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag();

            writer.RenderEndTag();

            writer.RenderEndTag();//tr


            writer.RenderEndTag();//table

            writer.RenderEndTag();//form

            writer.RenderEndTag();//Div
        }

        private void RenderInputField(HtmlTextWriter writer, string fieldName, int tabIndex, int maxLength, string description, string explain, bool isPassword)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "field");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            writer.AddAttribute(HtmlTextWriterAttribute.For, fieldName);
            writer.RenderBeginTag(HtmlTextWriterTag.Label);

            writer.Write(description);

            writer.AddAttribute(HtmlTextWriterAttribute.Id, fieldName + "Error");
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "error");
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.RenderEndTag();//Span

            writer.RenderEndTag();//label


            writer.AddAttribute(HtmlTextWriterAttribute.Type, isPassword ? "password" : "text");
            writer.AddAttribute(HtmlTextWriterAttribute.Id, fieldName);
            writer.AddAttribute(HtmlTextWriterAttribute.Name, fieldName);
            writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, tabIndex.ToString());
            writer.AddAttribute(HtmlTextWriterAttribute.Maxlength, maxLength.ToString());
            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag();

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "explain");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.Write(explain);
            writer.RenderEndTag();//div

            writer.RenderEndTag();//div
        }
    }
}
